﻿#include "WeaponBank.h"
#include"GuidedMissile.h"
#include"Sprite.h"
#include<qdebug.h>
WeaponBank::WeaponBank()
{
	init();
}

WeaponBank::WeaponBank(int max, QObject* parent) :m_max(max)
{
	init();
	m_parent = parent;
}

void WeaponBank::init()
{
	setType(Type::weapons);
	setTimer(100);
	m_BulletDemotion.setInterval(15_s);
	m_BulletDemotion.callOnTimeout([this]() //定时子弹降级
		{
			if (m_BulletLevel > 1) 
			{
				m_BulletLevel--;
			}
			if (m_BulletLevel == 1)
				m_BulletDemotion.stop();
		});

	m_GuidedMissileCD.setInterval(25_s);
	m_GuidedMissileCD.callOnTimeout([this]() //定时导弹CD
		{
			m_GuidedMissileCD.stop();
		});
	m_BulletFrameName = "子弹射击";
	//addBulletLevel(4);
	
}

void WeaponBank::addBullet(const QPoint& point)
{
	if (m_pause)
		return;
	if (m_bullet.count() >= m_max || !TimerTrigger())//大于上限
		return;
	switch (m_BulletLevel)
	{
	case 1:
		addOneBullet(m_vector,point); break;
	case 2:
	case 3:
	case 4:
		addTwoBullet(point); break;
	default:
		addThreeBullet(point);
		break;
	}
	
}

void WeaponBank::addBullet(const int x, const int y)
{
	addBullet(QPoint(x, y));
}

void WeaponBank::addGuidedMissile(const int x, const int y, Sprite& player)
{
	if (m_pause|| m_GuidedMissileCD.isActive())
		return;
	if (/*m_bullet.count() >= m_max ||*/ !TimerTrigger())//大于上限
		return;
	Bullet* Guided = new GuidedMissile(x,y,m_parent);
	m_bullet.append(Guided);//添加进武器库
	//qDebug() <<"add"<< getCollider();;
	if (getWindow().isNull())
	{
		OutInfo;
		qDebug() << "请在外部调用setWindow,缺少重要数据，程序无法继续运行,无法放出导弹";
		return;
	}
	Guided->setWindow(getWindow());//设置子弹边界自动释放
	//qDebug() << "ds13131" << bull.getCollider();
	Guided->setProperty(m_vector, m_Steplength*5);//设置子弹步长和便宜方向
	//qDebug() << "添加一个导弹" << "现有：" << m_bullet.count();
	player.reduceBombNum();
	m_GuidedMissileCD.start();
}

void WeaponBank::addOneBullet(const QVector2D& vector, const QPoint& point)
{
	Bullet* bullet = new Bullet(point);
	m_bullet.append(bullet);//添加进武器库

	//Bullet& bull=m_bullet.back();
	//qDebug() <<"add"<< getCollider();;
	if (getWindow().isNull())
	{
		OutInfo;
		qDebug() << "请在外部调用setWindow,缺少重要数据，程序无法继续运行,无法放出子弹";
		return;
	}
	bullet->setWindow(getWindow());//设置子弹边界自动释放
	//qDebug() << "ds13131" << bull.getCollider();
	bullet->setProperty(vector, m_Steplength);//设置子弹步长和便宜方向
	bullet->addFrame(m_BulletFrameName);
	//qDebug() << "添加一个子弹"<<"现有："<<m_bullet.count();
}

void WeaponBank::addTwoBullet(const QPoint& point)
{
	//int interval = 30;//子弹间隔
	int w = (m_BulletLevel-1) * m_Bulletinterval;//一排宽度
	int xleft = point.x() -w / 2.0;
	for (size_t i = 0; i < m_BulletLevel; i++)
	{
		addOneBullet(m_vector, xleft,point.y());
		xleft += m_Bulletinterval;
	}
}

void WeaponBank::addThreeBullet(const QPoint& point)
{
	int w = (m_BulletLevel - 1) * m_Bulletinterval;//一排宽度
	int xleft = point.x() - w / 2.0;
	int n = m_BulletLevel / 2;
	if (m_BulletLevel % 2)
	{
		addOneBullet(QVector2D(0, -1), point);
	}
	else
	{
		addOneBullet(QVector2D(0, -1), point.x()- m_Bulletinterval/2,point.y());
		addOneBullet(QVector2D(0, -1), point.x() + m_Bulletinterval / 2, point.y());
		n--;
	}
	{}
	for (size_t i = 0; i < n; i++)
	{
		addOneBullet(QVector2D(-1.0+1.0/n*i,-1), xleft, point.y());
		xleft += m_Bulletinterval;
	}
	xleft = point.x() + w / 2.0;
	for (size_t i = 0; i <n; i++)
	{
		addOneBullet(QVector2D(1.0 -1.0 / n * i, -1), xleft, point.y());
		xleft -= m_Bulletinterval;
	}
	
}

void WeaponBank::update()
{
	//子弹移动数据更新
	for (auto it= m_bullet.begin();it!= m_bullet.end(); it++)
	{
		(*it)->update();
		if (!(*it)->isActive())
		{
			delete (*it);
			it = m_bullet.erase(it);//删除不是活动的子弹
			if (it == m_bullet.end())
				break;
		}
	}
	//qDebug()<<m_BulletDemotion.remainingTime();
}

void WeaponBank::draw(QPainter* painter)
{
	//显示子弹
	for (auto& v : m_bullet)
	{
		v->draw(painter);
	}
}

void WeaponBank::setBulletFrame(const QString& name)
{
	m_BulletFrameName = name;
}

bool WeaponBank::Collider(const QRect& collider)
{
	for (auto& v : m_bullet)
	{
		if (v->Collider(collider))
		{
			return true;
		}
	}
	return false;
}
QVector<Bullet*>& WeaponBank::getBullet()
{
	return m_bullet;
}

size_t WeaponBank::getGuidedMissileCD()
{
	return m_GuidedMissileCD.interval();
}

qint32 WeaponBank::getGuidedMissileCdRemainingTime()
{
	return m_GuidedMissileCD.remainingTime();
}

void WeaponBank::setGuidedMissileCD(const size_t time)
{
	m_GuidedMissileCD.setInterval(time);
}

void WeaponBank::setBulletTimer(const int time)
{
	m_BulletTimer = time;
}

void WeaponBank::setBulletinterval(const int val)
{
	m_Bulletinterval = val;
}

void WeaponBank::setBulletDemotion(const int time)
{
	m_BulletDemotion.setInterval(time);
}

void WeaponBank::addBulletLevel(const int level, bool flag)
{
	//qDebug() << level<< m_BulletLevel;
	if (m_BulletLevel >= m_MaxBulletLevel)
		return;
	m_BulletLevel += level;
	if(flag|| !m_BulletDemotion.isActive())
	m_BulletDemotion.start();
}

const int WeaponBank::getBulletLevelRemainingTime()
{
	return m_BulletDemotion.remainingTime();
}

const int WeaponBank::getBulletLevel()
{
	return m_BulletLevel;
}

void WeaponBank::setMaxBulletLevel(const int level)
{
	m_MaxBulletLevel = level;
}

void WeaponBank::setPause(bool flag)
{
	Articles::setPause(flag);
	for (auto& v : m_bullet)
	{
		v->setPause(flag);
	}
}

bool WeaponBank::TimerTrigger()
{
	size_t size = (globalElapsedTimer.elapsed() / m_BulletTimer);
	if (size != m_temp)
	{
		m_temp = size;
		return true;
	}
	return false;
}

void WeaponBank::addOneBullet(const QVector2D& vector, const int x, const int y)
{
	addOneBullet(vector, QPoint(x, y));
}
